package com.das.mechanic_base.widget;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AlphaConfig;
import razerdp.util.animation.AnimationHelper;

/**
 * 评论输入框
 */
public class X3InputPopWindow extends BasePopupWindow implements TextWatcher {

    private EditText et_input;
    private ImageView iv_send;
    private String type;
    private Context mContext;

    public X3InputPopWindow(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public void onViewCreated(@NonNull View contentView) {
        MobclickAgent.onPageStart("留言输入弹窗");
        et_input = contentView.findViewById(R.id.et_input);
        iv_send = contentView.findViewById(R.id.iv_send);

        et_input.addTextChangedListener(this);
        iv_send.setOnClickListener(v -> {
            String content = et_input.getText().toString();
            if (X3StringUtils.isEmpty(content)) {
                return;
            }
            dismiss();
            if (iOnSendContent != null) {
                iOnSendContent.iOnSendContent(content,type);
            }
        });
    }

    @Override
    public void onDismiss() {
        MobclickAgent.onPageEnd("留言输入弹窗");
        if (et_input != null) {
            et_input.setText("");
        }
        super.onDismiss();
    }

    IOnSendContent iOnSendContent;

    public void setiOnSendContent(IOnSendContent iOnSendContent) {
        this.iOnSendContent = iOnSendContent;
    }

    /**
     * 0 留言
     * 1 修改名字
     * @param i
     */
    public void showType(String i,String content,long limit) {
        this.type = i;
        if ("lastName".equals(type) || "home".equals(type) || "nickname".equals(type) || "workPlace".equals(type)) {
            et_input.setHint(mContext.getString(R.string.x3_please_input));
        }else {
            et_input.setHint(mContext.getString(R.string.x3_ground_push_input));
        }

        if (limit != 0) {
            et_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter((int) limit)});
        }else {
            et_input.setFilters(new InputFilter[]{});
        }
        if (X3StringUtils.isEmpty(content)) {
            return;
        }
        et_input.setText(content + "");
        et_input.setSelection(content.length());
    }

    public interface IOnSendContent {
        void iOnSendContent(String content,String type);
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.x3_ground_push_input_item);
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.IN.duration(200))
                .toShow();
    }


    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.OUT.duration(200))
                .toDismiss();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String content = s.toString();
        if (X3StringUtils.isEmpty(content)) {
            iv_send.setImageResource(R.mipmap.x3_ground_not_send);
        }else {
            iv_send.setImageResource(R.mipmap.x3_ground_send);
        }
    }

}
